package com.kangyi.admin.controller;

import com.kangyi.admin.service.EmployeeService;
import com.kangyi.dto.AddEmployeeDTO;
import com.kangyi.dto.EditEmployeeDTO;
import com.kangyi.dto.EmployeePageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.EmployeeDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "养老院-员工管理相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/page")
    @ApiOperation("员工管理分页查询")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工管理分页查询:{}",employeePageQueryDTO);
        PageResult pageResult = null;
        try {
            pageResult = employeeService.pageQuery(employeePageQueryDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success(pageResult,"查询成功！");
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除员工")
    @CacheEvict(cacheNames = "employee_", allEntries = true)
    public Result deleteEmployee(@RequestParam List<Integer> ids){
        log.info("批量删除员工:{}",ids);
        try {
            employeeService.deleteEmployee(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("删除成功！");
    }

    @PostMapping("/add")
    @ApiOperation("新增员工")
    @CacheEvict(cacheNames = "employee_", allEntries = true)
    public Result addEmployee(AddEmployeeDTO addEmployeeDTO){
        log.info("新增员工");
        try {
            employeeService.addEmployee(addEmployeeDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("新增成功");
    }


    @PutMapping("/edit")
    @ApiOperation("修改员工信息")
    @CacheEvict(cacheNames = "employee_", allEntries = true)
    public Result editEmployee(@RequestBody EditEmployeeDTO editEmployeeDTO){
        log.info("修改员工信息:{}",editEmployeeDTO);
        try {
            employeeService.editEmployee(editEmployeeDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/detail/{employeeId}")
    @ApiOperation("查询员工详情")
//    @Cacheable(cacheNames = "employee_", key = "'adminDetail_'+#employeeId")
    public Result<EmployeeDetailVO> detailEmployee(@PathVariable Integer employeeId){
        log.info("查询员工详情:{}",employeeId);
        try {
            EmployeeDetailVO employeeDetailVO = employeeService.detailEmployee(employeeId);
            return Result.success(employeeDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
